package com.prs.crm.action.hr;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.List;
import java.util.Properties;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.courser.struts.BaseAction;
import org.courser.ui.PaginationSupport;
import org.hibernate.criterion.DetachedCriteria;

import com.prs.crm.annotation.Clew;
import com.prs.crm.context.Constrains;
import com.prs.crm.context.ToolKits;
import com.prs.crm.domain.cdxh.HrPerson;
import com.prs.crm.domain.hr.JobTitle;
import com.prs.crm.domain.hr.Resume;
import com.prs.crm.domain.hr.Role;
import com.prs.crm.domain.hr.ShareEim;
import com.prs.crm.service.cdxh.HrPersonService;
import com.prs.crm.service.hr.JobTitleService;
import com.prs.crm.service.hr.ResumeService;
import com.prs.crm.service.hr.RoleService;
import com.prs.crm.service.person.ShareEimService;

public class ResumeAction extends BaseAction {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private HrPerson hrPerson;
	private HrPersonService hrPersonService;
	private Resume resume;
	private ResumeService resumeService;
	private PaginationSupport pagination;
	private Integer[] resumeIds;
	private File photoImg;
	private String photoImgFileName;
	private File resumeAttach;
	private String resumeAttachFileName;

	private List<Role> roles;
	private RoleService roleService;

	private List<JobTitle> titles;
	private JobTitleService jobTitleService;

	private String bgName;
	private String bgdeepd;
	private Connection con;
	private Statement stmt;
	private ResultSet rs;
	private ResultSet rs1;
	private ResultSet rs2;
	private ResultSet rs3;
	private String ClassName;
	private String userName;
	private String userPass;
	private String url1;
	private ShareEimService shareEimService;
	private String address;
	private String baseName;
	public ShareEimService getShareEimService() {
		return shareEimService;
	}

	public void setShareEimService(ShareEimService shareEimService) {
		this.shareEimService = shareEimService;
	}

	public List<Role> getRoles() {
		return roles;
	}

	public void setRoles(List<Role> roles) {
		this.roles = roles;
	}

	public RoleService getRoleService() {
		return roleService;
	}

	public void setRoleService(RoleService roleService) {
		this.roleService = roleService;
	}

	public List<JobTitle> getTitles() {
		return titles;
	}

	public void setTitles(List<JobTitle> titles) {
		this.titles = titles;
	}

	public static long getSerialVersionUID() {
		return serialVersionUID;
	}

	public File getPhotoImg() {
		return photoImg;
	}

	public void setPhotoImg(File photoImg) {
		this.photoImg = photoImg;
	}

	public String getPhotoImgFileName() {
		return photoImgFileName;
	}

	public void setPhotoImgFileName(String photoImgFileName) {
		this.photoImgFileName = photoImgFileName;
	}

	public File getResumeAttach() {
		return resumeAttach;
	}

	public void setResumeAttach(File resumeAttach) {
		this.resumeAttach = resumeAttach;
	}

	public String getResumeAttachFileName() {
		return resumeAttachFileName;
	}

	public void setResumeAttachFileName(String resumeAttachFileName) {
		this.resumeAttachFileName = resumeAttachFileName;
	}

	@Action("listResume")
	public String execute() {
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(
				Resume.class);
		pagination = this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.getResumeService().listByPage(criteria,
				pagination));
		return SUCCESS;
	}

	@Action("selectResume")
	public String selectResume() {
		this.execute();
		return SUCCESS;
	}

	@Action("viewResume")
	public String view() {
		return SUCCESS;
	}

	@Action("alterResume")
	public String alter() {
		this.execute();
		return SUCCESS;
	}

	@Action("editResume")
	public String edit() {
		this.roles = this.getRoleService().getAllRoles();
		this.titles = this.getJobTitleService().getAllJobTitle();

		return SUCCESS;
	}

	@Clew(results = { "success", "none" })
	@Action(value = "saveResume", results = {
			@Result(name = "success", type = "redirect", location = "editResume"),
			@Result(name = "none", type = "redirect", location = "alterResume"),
			@Result(name = "error", type = "chain", location = "editResume") })
	public String save() throws SQLException {
		if (resume.getName() == null || "".equals(resume.getName().trim())) {
			addActionError("姓名不能为空");
			return ERROR;
		}

		// if (isnull(resume.getCode())) {
		// if(!ToolKits.isInteger(resume.getCode())){
		// addActionError("身份证号码只能是数字类型");
		// return ERROR;
		// }
		// if (resume.getCode().trim().length() != 15
		// && resume.getCode().trim().length() != 18) {
		// addActionError("身份证号码只能有15位或者18位");
		// return ERROR;
		// }
		//
		// String code =
		// "[\\d]{6}(19|20)*[\\d]{2}((0[1-9])|(10|11|12))([012][\\d]|(30|31))[\\d]{3}[xX\\d]";
		// Pattern codep = Pattern.compile(code);
		// Matcher codem = codep.matcher(resume.getCode().trim());
		// if (!codem.matches()) {
		// addActionError("身份证号码不正确");
		// return ERROR;
		// }
		// }

		if (resume.getSex() == null || "".equals(resume.getSex().trim())) {
			addActionError("性别不能为空");
			return ERROR;
		}
		if (resume.getRole() == null) {
			addActionError("职位不能为空");
			return ERROR;
		}
		if (resume.getTitle() == null) {
			addActionError("职称不能为空");
			return ERROR;
		}

		 if (!ToolKits.isInteger(resume.getPostcode().trim())
		 || resume.getPostcode().trim().length() != 6) {
		 addActionError("邮编只能为数字并且长度只能为6");
		 return ERROR;
		 }
		if (isnull(resume.getPhone_No())
				&& !ToolKits.isInteger(resume.getPhone_No().trim())) {
			addActionError("联系电话只能是数字类型");
			return ERROR;
		}
		if (isnull(resume.getMobile_No())
				&& !ToolKits.isInteger(resume.getMobile_No().trim())) {
			addActionError("手机号码只能是数字类型");
			return ERROR;
		}
		if (isnull(resume.getQq_No())
				&& !ToolKits.isInteger(resume.getQq_No().trim())) {
			addActionError("QQ号码只能是数字类型");
			return ERROR;
		}
		 if (isnull(resume.getEmail())) {
		 if (emailFormat(resume.getEmail())) {
		 this.addActionError("邮箱格式不对!请重新输入");
		 return ERROR;
		 }
		 }
		if (resume.getId() == null) {
//			HrPerson hdObject = this.getHrPersonService().getHrPersonDao(
//					resume.getName(), resume.getRole().getName());
//			if (hdObject == null) {
//				HrPerson gh = new HrPerson();
//				gh.setHrDepartment(resume.getRole().getName());
//				gh.setHrName(resume.getName());
//				this.getHrPersonService().save(gh);
//			}

			this.setPrompt("提交简历成功");
			resume.setState(Constrains.TASK_AUDITING);
			this.getResumeService().saveResume(resume, photoImg,
					photoImgFileName, resumeAttach, resumeAttachFileName);
			return SUCCESS;
		} else {
			this.setPrompt("修改简历成功");
//			HrPerson hpObject = this.getHrPersonService().getHrPersonDao(bgName,bgdeepd);
//			if(hpObject!=null){
//				hpObject.setHrName(resume.getName());	
//				hpObject.setHrDepartment(resume.getRole().getName());
//				this.getHrPersonService().submit(hpObject);
//			}
			this.getResumeService().saveResume(resume, photoImg,
					photoImgFileName, resumeAttach, resumeAttachFileName);
			return NONE;
		}
	}

	@Action(value = "removeResume", results = { @Result(name = "success", type = "redirect", location = "listResume") })
	public String remove() {
		if (resumeIds != null) {

			// 生产人员删除
			for (int i = 0; i < resumeIds.length; i++) {
				Resume reObject = this.getResumeService().get(resumeIds[i]);
				if (reObject != null) {
					HrPerson hpObject = this.getHrPersonService()
							.getHrPersonDao(reObject.getName(),
									reObject.getRole().getName());
					if (hpObject != null) {
						this.getHrPersonService().remove(hpObject);
					}
				}

			}
			this.getResumeService().batchRemove(resumeIds);
		}
		return SUCCESS;
	}

	public Resume getResume() {
		return resume;
	}

	public void setResume(Resume resume) {
		this.resume = resume;
	}

	public ResumeService getResumeService() {
		return resumeService;
	}

	public void setResumeService(ResumeService resumeService) {
		this.resumeService = resumeService;
	}

	public PaginationSupport getPagination() {
		return pagination;
	}

	public void setPagination(PaginationSupport pagination) {
		this.pagination = pagination;
	}

	public Integer[] getResumeIds() {
		return resumeIds;
	}

	public void setResumeIds(Integer[] resumeIds) {
		this.resumeIds = resumeIds;
	}

	public void setJobTitleService(JobTitleService jobTitleService) {
		this.jobTitleService = jobTitleService;
	}

	public JobTitleService getJobTitleService() {
		return jobTitleService;
	}

	public HrPerson getHrPerson() {
		return hrPerson;
	}

	public void setHrPerson(HrPerson hrPerson) {
		this.hrPerson = hrPerson;
	}

	public HrPersonService getHrPersonService() {
		return hrPersonService;
	}

	public void setHrPersonService(HrPersonService hrPersonService) {
		this.hrPersonService = hrPersonService;
	}

	public boolean isnull(String str) {
		boolean s = true;
		if (str.trim().equals("") || str == null) {
			s = false;
		}
		return s;
	}

	public boolean emailFormat(String email) {
		boolean tag = false;
		final String pattern1 = "^([a-z0-9A-Z]+[-|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$";
		final Pattern pattern = Pattern.compile(pattern1);
		final Matcher mat = pattern.matcher(email);
		if (!mat.find()) {
			tag = true;
		}
		return tag;
	}

	public String getBgName() {
		return bgName;
	}

	public void setBgName(String bgName) {
		this.bgName = bgName;
	}

	public String getBgdeepd() {
		return bgdeepd;
	}

	public void setBgdeepd(String bgdeepd) {
		this.bgdeepd = bgdeepd;
	}
	public void saveEimResume(Resume resume) throws SQLException{
		ShareEim se=this.getShareEimService().getCode("se01");
		if (se!=null) {
			ClassName=se.getClassName();
			userName=se.getUserName();
			userPass=se.getUserPass();
			address=se.getIp();
			baseName=se.getBaseName();
			url1="jdbc:mysql://"+address+":3306/"+baseName;
		}
		try {
			Class.forName(ClassName);
			try {
				con = DriverManager.getConnection(url1, userName, userPass);
			} catch (SQLException e) {
				e.printStackTrace();
			}
			try {
				stmt = this.con.createStatement();
			} catch (SQLException e) {
				e.printStackTrace();
			}
			rs=stmt.executeQuery("select * from hr_resume where name='"+resume.getName()+"'");
			if (rs.next()==false) {
				String sql1 = "insert into hr_resume(address,code,college,MSN_No,QQ_No,email,mobile_No,phone_No,degree,demandsalary,hobby,name,nationality,party,postcode,race,religion,sex," +
						"speciality,state,familyInfo,social,source) values ('"+resume.getAddress()+"','"+resume.getCode()+"','"+resume.getCollege()+"','"+resume.getMsn_No()+"','"+resume.getQq_No()+"','"+resume.getEmail()+"','"+resume.getMobile_No()+"','"+resume.getPhone_No()+"','"+resume.getDegree()
						+"','"+resume.getDemandsalary()+"','"+resume.getHobby()+"','"+resume.getName()+"','"+resume.getNationality()+"','"+resume.getParty()+"','"+resume.getPostcode()+"','"+resume.getRace()+"','"+resume.getReligion()+"','"+resume.getSex()+"','"+resume.getSpeciality()+"','"+resume.getState()+"','"+resume.getFamilyInfo()+"','"+resume.getSocial()+"','"+resume.getSource()
						+"')";
				System.out.println(sql1);
				stmt.executeUpdate(sql1);
			}
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
	}
}
